package com.kylin.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration//配置类
@EnableSwagger2//开启swagger2
public class SwaggerConfig {

    //配置了Swagger的Docket的bean实例
    @Bean
    public Docket docket(Environment environment) {
        //设置要显示的Swagger环境
        Profiles profiles = Profiles.of("dev", "test");
        //通过environment.acceptProfiles判断是否处在自己设定的环境中
        boolean flag = environment.acceptsProfiles(profiles);
        System.out.println(flag);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //enable：是否启动swagger 为false 不能浏览器中访问
                .enable(flag)
                .groupName("Kylin")
                .select()
                //RequestHandlerSelectors 配置要扫描接口的方式
                //basePackage():指定要扫描的包
                //any():扫描全部
                //none():不扫描
                //withClassAnnotation():扫描类上的注解 参数是一个注解的反射对象
                //withMethodAnnotation():扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.kylin.controller"))
                //paths()过滤什么路径
                //.paths(PathSelectors.ant("/kylin/**"))
                .build();
    }

    //配置Swagger信息 apiInfo
    private ApiInfo apiInfo() {
        Contact contact = new Contact("Kylin", "https://www.kylin.show", "zhang171346168@qq.com");
        return new ApiInfo(
                "Kylin的SwaggerAPI文档",//标题
                "学习不易，努力努力~",//描述
                "v1.0",//版本
                "https://www.kylin.show",//组织链接
                contact,//联系人信息
                "Apache 2.0",//许可
                "http://www.apache.org/licenses/LINCENSE-2.0",//许可链接
                new ArrayList());
    }
}
